package org.streets.workflow.engine.entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

import org.streets.commons.util.GUID;
import org.streets.database.annotations.NestedFetch;
import org.streets.workflow.engine.IProcessInstance;
import org.streets.workflow.model.WFProcess;

/**
 * ProcessInstance generated by hbm2java
 */
@SuppressWarnings("serial")
@Entity 
@Table(name="WF_RT_PROCESS")
public class ProcessInstance implements IProcessInstance, Serializable {
	@Id
    private String id = null;
	@Column
    private String definitionId = null;
	@Column	
    private Integer version = null;
	@Column
    private String name = null;
	@Column
    private Integer state = null;
	@Column
    private Boolean suspended = null;
	@Column
    private String creatorId = null;
	@Column
    private Date createdTime = null;
	@Column
    private Date startedTime = null;
	@Column
    private Date endTime = null;
	@Column
    private Date expiredTime = null;
	@Column
    private String parentId = null;
	@Column
    private String parentTaskId = null;
	@NestedFetch(
			type=ProcessVariant.class, 
			value="select * from WF_RT_PROCESS_VAR where process_id = :id"
	)
	private List<ProcessVariant> variables = new ArrayList<ProcessVariant>();

	
    private transient Map<String, Object> _var_map = new HashMap<String, Object>();
    private transient WFProcess model = null;

    public ProcessInstance() {
        this.state = IProcessInstance.INITIALIZED; //IProcess.INITIALIZED;
        this.suspended = false;
    }

    public String getId() {
        return this.id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getDefinitionId() {
        return this.definitionId;
    }

    public void setDefinitionId(String processID) {
        this.definitionId = processID;
    }

    public Integer getVersion() {
        return version;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getState() {
        return this.state;
    }

    public void setState(Integer state) {
        this.state = state;
    }

    public String getParentId() {
        return parentId;
    }

    public void setParentId(String parentProcessId) {
        this.parentId = parentProcessId;
    }
    
    public List<ProcessVariant> getVariables() {
		return variables;
	}

	public void setVariables(List<ProcessVariant> variables) {
		this.variables = variables;
	}

	public Object getVariable(String name) {
		if (_var_map.size() == 0) {
			for (ProcessVariant var : variables) {
				_var_map.put(var.getName(), var.getValue());
			}
		}
		return _var_map.get(name);
	}
	
	public void setVariable(String name, Object value) {
	    
	    boolean n_v = true;
		
	    for (ProcessVariant var : variables) {
			if (var.getName().equals(name)) {
				var.setValue(value);
				n_v = true;
				break;
			}
		}
		// 如果是新的变量就创建
		if (n_v) {
		    ProcessVariant var = new ProcessVariant();
		    var.setId(GUID.nextUUID());
		    var.setName(name);
		    var.setValue(value);
		    var.setProcessId(getId());
		}
		_var_map.put(name, value);
	}

    public String getParentTaskId() {
        return parentTaskId;
    }

    public void setParentTaskId(String taskInstId) {
        parentTaskId = taskInstId;
    }

    public Date getCreatedTime() {
        return this.createdTime;
    }

    public Date getStartedTime() {
        return this.startedTime;
    }

    public Date getEndTime() {
        return this.endTime;
    }

    public void setCreatedTime(Date createdTime) {
        this.createdTime = createdTime;
    }

    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }

    public void setStartedTime(Date startedTime) {
        this.startedTime = startedTime;
    }

    public Date getExpiredTime() {
        return this.expiredTime;
    }

    public void setExpiredTime(Date arg) {
        this.expiredTime = arg;
    }

    public String getCreatorId() {
        return this.creatorId;
    }

    public void setCreatorId(String s) {
        this.creatorId = s;
    }

    public Boolean isSuspended() {
        return suspended;
    }
    
    public Boolean getSuspended(){
    	return suspended;
    }

    public void setSuspended(Boolean isSuspended) {
        this.suspended = isSuspended;
    }
        
    public WFProcess getModel() {
        return model;
    }

    public void setModel(WFProcess model) {
        this.model = model;
        setDefinitionId(model.getId());
        setName(model.getDisplayName());        
    }

}
